package com.jiangroom.ribbon.service;

import com.jiangroom.ribbon.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author dengzaiqiang
 * @createTime 2018/4/13
 * @description hello
 */
@Service
public class HelloService {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "helloFallback")
    public String hello() {

        String result = restTemplate.getForEntity("http://hello-service/hello", String.class).getBody();
        return result;
    }

    public String helloFallback() {
        return "hello fallback";
    }

    /**
     * CacheResult 缓存结果
     * CacheKey 使用id的值为key
     * 此缓存只在同一个request中多次请求相同接口，才会使用缓存，同redis缓存不一样
     * @param id
     * @return
     */
    //@CacheResult
    @HystrixCommand
    public User getUserById(Long id){
        return restTemplate.getForObject("http://hello-service/getUserById/{1}",User.class,id);
    }
}
